﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Win8StorageHelper;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace StorageHelperTest
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private const string imageUrl = "http://www.blogcdn.com/www.engadget.com/media/2012/06/microsoftsurfacetablet.jpg";
        private string fileName = string.Empty;

        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private async void CreateFileClick(object sender, RoutedEventArgs e)
        {
            fileName = txtFileName.Text;

            if (!string.IsNullOrEmpty(fileName))
            {
                fileName = fileName + ".txt";
                var fileContent = string.Format("Text file created at: ", DateTime.Now.ToString());
                StorageHelper.CreateTextFileAsync(KnownFolders.DocumentsLibrary, fileName, fileContent);
            }
            else
            {
                var dialog = new MessageDialog("Please add file name in the TextBox.", "Creating Text File");
                await dialog.ShowAsync();
            }
        }

        private async void ReadFileClick(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(fileName))
            {
               string fileContent = await StorageHelper.ReadTextFileAsync(KnownFolders.DocumentsLibrary, fileName);
               var dialog = new MessageDialog(fileContent, string.Format("File Content of File: {0}", fileName));
               await dialog.ShowAsync();
            }
            else
            {
                var dialog = new MessageDialog("Please create a file first.", "Creating Text File");
                await dialog.ShowAsync();
            }
        }

        private void SaveImageClick(object sender, RoutedEventArgs e)
        {
            StorageHelper.SaveImageInPicturesLibrary(imageUrl);
        }
    }
}
